package cn.com.cifi.mars.service.impl;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.com.cifi.mars.common.exception.MsgException;
import cn.com.cifi.mars.constant.CodeEnum;
import cn.com.cifi.mars.entity.PanoplanBuildPlanItemDetail;
import cn.com.cifi.mars.mapper.mars.PanoplanBuildPlanItemDetailMapper;
import cn.com.cifi.mars.service.PanoplanBuildPlanItemDetailService;
import cn.com.cifi.mars.util.UUIDUtils;

/**
 * <p>
 * 楼栋子表-组团楼栋-详情表 服务实现类
 * </p>
 *
 * @author yyd
 * @since 2019-09-07
 */
@Service
public class PanoplanBuildPlanItemDetailServiceImpl extends ServiceImpl<PanoplanBuildPlanItemDetailMapper, PanoplanBuildPlanItemDetail> implements PanoplanBuildPlanItemDetailService {

	private static final Logger log = LoggerFactory.getLogger(PanoplanBuildPlanItemDetailServiceImpl.class);
	 
    @Autowired
    PanoplanBuildPlanItemDetailMapper panoplanBuildPlanItemDetailMapper;
    @Override
    public void syncPanoplanBuildPlanItemDetail(JSONObject panoplanBuildPlanItemDetail, String pkId,String itemDetailId) {
        try {
            if (null == panoplanBuildPlanItemDetail || panoplanBuildPlanItemDetail.isEmpty() || null == pkId){
                throw new MsgException(CodeEnum.REQUEST_PARAM_NOT_FULL_ERROR);
            }else{
               // String id = UUIDUtils.create();
                String id=itemDetailId;
            	PanoplanBuildPlanItemDetail newPanoplanBuildPlanItemDetail = new PanoplanBuildPlanItemDetail();
                newPanoplanBuildPlanItemDetail.setId(id);
                newPanoplanBuildPlanItemDetail.setPkId(pkId);
                newPanoplanBuildPlanItemDetail.setVersionId((String) panoplanBuildPlanItemDetail.get("versinID"));
                newPanoplanBuildPlanItemDetail.setUniqueCode((String) panoplanBuildPlanItemDetail.get("uniqueCode"));
                newPanoplanBuildPlanItemDetail.setNodeCode((String) panoplanBuildPlanItemDetail.get("nodeCode"));
                newPanoplanBuildPlanItemDetail.setNodeName((String) panoplanBuildPlanItemDetail.get("nodeName"));
                Object actualFinishDate = panoplanBuildPlanItemDetail.get("actualFinishDate");
                Object plannedFinishDate = panoplanBuildPlanItemDetail.get("plannedFinishDate");
                if (null != actualFinishDate){
                    LocalDateTime newActualFinishDate = new Date(Long.parseLong(actualFinishDate.toString())).toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
                    newPanoplanBuildPlanItemDetail.setActualFinishDate(newActualFinishDate);
                }
                if (null != plannedFinishDate){
                    LocalDateTime newPlannedFinishDate = new Date(Long.parseLong(plannedFinishDate.toString())).toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
                    newPanoplanBuildPlanItemDetail.setPlannedFinishDate(newPlannedFinishDate);
                }
                newPanoplanBuildPlanItemDetail.setCreatedDate(LocalDateTime.now());
                newPanoplanBuildPlanItemDetail.setIsSendBack(0);
    //        panoplanBuildPlanItemDetailMapper.syncPanoplanBuildPlanItemDetail(newPanoplanBuildPlanItemDetail);
                panoplanBuildPlanItemDetailMapper.insert(newPanoplanBuildPlanItemDetail);
            }
        } catch (MsgException e) {
            throw new MsgException(CodeEnum.REQUEST_PARAM_NOT_FULL_ERROR);
        }
    }

    @Override
    @Transactional
    public void syncAllDataItemDetail(Map<String, List<JSONObject>> nodeDetailMap) {
        try {
            if (!nodeDetailMap.isEmpty()){
                String pkId = null;
                List<JSONObject> nodeDetailItemList = null;
                List<PanoplanBuildPlanItemDetail> entityList = new ArrayList<>();
                Iterator<Map.Entry<String, List<JSONObject>>> iterator = nodeDetailMap.entrySet().iterator();
                while (iterator.hasNext()){
                    Map.Entry<String, List<JSONObject>> entry = iterator.next();
                    pkId = entry.getKey();
                    nodeDetailItemList = entry.getValue();
                    for (JSONObject nodeDetailItemJsonObject : nodeDetailItemList) {
                        PanoplanBuildPlanItemDetail panoplanBuildPlanItemDetail = new PanoplanBuildPlanItemDetail();
                        panoplanBuildPlanItemDetail.setId(UUIDUtils.create());
                        panoplanBuildPlanItemDetail.setPkId(pkId);
                        panoplanBuildPlanItemDetail.setVersionId((String) nodeDetailItemJsonObject.get("versinID"));
                        panoplanBuildPlanItemDetail.setUniqueCode((String) nodeDetailItemJsonObject.get("uniqueCode"));
                        panoplanBuildPlanItemDetail.setNodeCode((String) nodeDetailItemJsonObject.get("nodeCode"));
                        panoplanBuildPlanItemDetail.setNodeName((String) nodeDetailItemJsonObject.get("nodeName"));
                        if (null != nodeDetailItemJsonObject.get("plannedFinishDate")){
                            panoplanBuildPlanItemDetail.setPlannedFinishDate(objToDate(nodeDetailItemJsonObject.get("plannedFinishDate")));
                        }
                        if (null != nodeDetailItemJsonObject.get("actualFinishDate")){
                            panoplanBuildPlanItemDetail.setActualFinishDate(objToDate(nodeDetailItemJsonObject.get("actualFinishDate")));
                        }
                        if (null != nodeDetailItemJsonObject.get("createdDate")){
                            panoplanBuildPlanItemDetail.setCreatedDate(objToDate(nodeDetailItemJsonObject.get("createdDate")));
                        }
                        if (null != nodeDetailItemJsonObject.get("updatedDate")){
                            panoplanBuildPlanItemDetail.setUpdatedDate(objToDate(nodeDetailItemJsonObject.get("updatedDate")));
                        }
                        if (null != nodeDetailItemJsonObject.get("isSendBack")){
                            panoplanBuildPlanItemDetail.setIsSendBack(Integer.parseInt(nodeDetailItemJsonObject.get("isSendBack").toString()));
                        }
                        entityList.add(panoplanBuildPlanItemDetail);
                    }
                }

                panoplanBuildPlanItemDetailMapper.syncAllDataItemDetail(entityList);
            }else{
                throw new MsgException(CodeEnum.REQUEST_PARAM_NOT_FULL_ERROR);
            }
        } catch (Exception e) {
            log.error("PanoplanBuildPlanItemDetailServiceImpl.syncAllDataItemDetail-->"+e);
        }
    }

    private LocalDateTime objToDate(Object date) {
        try {
            LocalDateTime parseTime = null;
            if ( null != date){
                String dateStr = date.toString();
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                parseTime = LocalDateTime.parse(dateStr, formatter);
            }
            return parseTime;
        } catch (Exception e) {
            throw new MsgException(CodeEnum.REQUEST_PARAM_NOT_FULL_ERROR);
        }
    }
}
